/**
 * Project Name:renshuaipeng-parent
 * File Name:BeetlConfig
 * Package Name:ren.renshuaipeng.search.conf
 * Date:15-12-13
 * Copyright (c) 2015, niao.shuai123@163.com All Rights Reserved.
 */

package cn.apiManager.web.conf;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.beetl.core.Function;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.fn.DateFunction;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * ClassName:BtlConfig <br/>
 * Function: btl 配置文件. <br/>
 * Date: 15-12-13 <br/>
 * 
 * @author 任帅鹏（作者）
 * @version V1.00 （版本号）
 * @since JDK 1.8
 * @see
 */
@Configuration
public class BtlConfig {

	private static final Logger logger = LoggerFactory.getLogger(BtlConfig.class);

	@Bean(initMethod = "init", name = "beetlConfig")
	public BeetlGroupUtilConfiguration getBtlGroupUtilConfiguration() throws IOException {

		BeetlGroupUtilConfiguration beetlConfig = new BeetlGroupUtilConfiguration();
		ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader("/templates");
		beetlConfig.setResourceLoader(classpathResourceLoader);
		// 初始化数据
		Map<String, Function> functionMap = new HashMap<>();
		functionMap.put("dateFunc", new DateFunction());
		beetlConfig.setFunctions(functionMap);
		// 读取配置文件信息
		logger.info("BtlConfig 配置");
		return beetlConfig;
	}

	@Bean(name = "viewResolver")
	public BeetlSpringViewResolver getBtlSpringViewResolver(@Value("${spring.mvc.view.prefix}") String prefix,
			@Value("${spring.mvc.view.suffix}") String suffix,
			@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlConfig) {
		BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
		beetlSpringViewResolver.setPrefix(prefix);
		beetlSpringViewResolver.setSuffix(suffix);
		beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
		beetlSpringViewResolver.setConfig(beetlConfig);
		return beetlSpringViewResolver;
	}
}
